package pe.org.cnl.congreso.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.persistence.*;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.GrantedAuthorityImpl;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;



/**
 * The persistent class for the usuario database table.
 * 
 */
@Entity
@Table(name="usuario")
public class Usuario extends User implements Serializable {
	private static final long serialVersionUID = 1L;

	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	private int idUsuario;

	private String clave;

	private int estado;

	private String username;

	   public Usuario() {
	    	super("default", "default", true, true, true, true , uno() );		
	    }
		
		public static List<GrantedAuthority> uno(){
			List<GrantedAuthority> oo = new ArrayList<GrantedAuthority>(); 
			oo.add(new GrantedAuthorityImpl("IS_AUTHENTICATED_ANONYMOUSLY") );
			return oo;
		}
		
	    public static Usuario getUsuarioBean() {
	    	Usuario nu = (Usuario)(SecurityContextHolder.getContext().getAuthentication().getPrincipal());
			if(nu != null) {
				return nu;
			}
			else return null;
		}
		
	    public Usuario(String username, String password, boolean enabled,Collection<GrantedAuthority> authorities) {
			super(username, password, enabled, true, true, true, authorities);
			this.estado = enabled==true ? 1 : 0;
			this.username = username;
			this.clave = password;
		}

	public int getIdUsuario() {
		return this.idUsuario;
	}

	public void setIdUsuario(int idUsuario) {
		this.idUsuario = idUsuario;
	}

	public String getClave() {
		return this.clave;
	}

	public void setClave(String clave) {
		this.clave = clave;
	}

	public int getEstado() {
		return this.estado;
	}

	public void setEstado(int estado) {
		this.estado = estado;
	}

	public String getUsername() {
		return this.username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

}